package me.zixing.java.chapter12;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * Created by bingh on 2017/12/13.
 * 练习：
 *  JDBC
 */
public class Main {

    private static String url = "jdbc:mysql:///bootdo";
    private static String userName = "bingh";
    private static String passwd = "bingh";
    private static Connection connection;
    static {
        try{
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取连接
            connection = DriverManager.getConnection(url,userName,passwd);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception{
        //创建语句
        PreparedStatement preparedStatement = connection.prepareStatement("SHOW TABLES;");
        //执行查询，返回结果集
        ResultSet resultSet =
                preparedStatement.executeQuery();
        //遍历结果集
        while(resultSet.next()){
            //获取每条记录的某个字段
            String tableName = resultSet.getString(1);
            System.out.println(tableName);
        }

    }
}
